﻿@namespace CyssBlazor.Shared.Components
@inherits PictureBase
<a href="@Url">@FileName</a>
@code {

    /// <summary>
    /// 图片Id
    /// </summary>
    [Parameter]
    public int Value { set; get; }

    /// <summary>
    /// 链接
    /// </summary>
    [Parameter]
    public string Url { set; get; }

    /// <summary>
    /// 文件名称
    /// </summary>
    public string FileName { set; get; }

    public override async Task SetParametersAsync(ParameterView parameters)
    {
        var newValue = parameters.GetValueOrDefault<int>(nameof(this.Value));
        var oldValue = this.Value;
        await base.SetParametersAsync(parameters);
        if (newValue != oldValue)
        {
            await LoadFileName();
        }
    }

    /// <summary>
    /// 初始加载图片
    /// </summary>
    private async Task LoadFileName()
    {
        if (this.Value > 0)
        {
            await InvokeAsync(StateHasChanged);
            var operateResult = await CommonClientFactory.Download.GetDownloadById(this.Value);
            if (operateResult.IsSuccess)
            {
                this.FileName = operateResult.Data.FileName + operateResult.Data.Extension;
                this.Url = $"http://localhost:55151/Download/GetFileById?Id={this.Value}";
            }
            await InvokeAsync(StateHasChanged);
        }
        else
        {
            this.FileName = string.Empty;
        }
    }
}
